<?php

namespace rocotito\businessLogic;

use rocotito\entity\place;
use rocotito\entity\address;
use rocotito\entity\typefood;
use connection\MySQL;

include 'entity.php';
include_once('mysql.php');
 
class BL_address {
	
	private $address = array();
	
	public function BL_address($place_cod_place){
		$result = $this->query_address_by_id($place_cod_place);
		while ($row = mysql_fetch_array($result,MYSQL_BOTH)) {
			$address = new address();
			$address->address($row['cod_address'],
										   $row['place_cod_place'],
										   $row['address_line_1'],
										   $row['address_line_2'],
										   $row['zip'],
										   $row['reference'],
										   $row['phone']);
			$this->address[] = $address;
		}
		
	}
	
	/**
	 * @return the $address
	 */
	public function getAddress() {
		return $this->address;
	}

	/**
	 * @param multitype: $address
	 */
	public function setAddress($address) {
		$this->address = $address;
	}
	
	public function query_address_by_id($place_cod_place){
		$mysql = new MySQL();
		$mysql->MySQL();
		$query = 'SELECT cod_address,place_cod_place,address_line_1,address_line_2,state_province_region,zip,reference,phone FROM address WHERE 1=1 AND place_cod_place='.$place_cod_place;
		$result = $mysql->query($query);
		return $result;
	}

	
	
}

class BL_typefood {
	private $typefood = array();
	/**
	 * @return the $typefood
	 */
	
	public function BL_typefood($place_cod_place){
		$result = $this->query_typefood_by_id($place_cod_place);
		while ($row = mysql_fetch_array($result,MYSQL_BOTH)) {
			$typefood = new typefood();
			$typefood->typefood($row['cod_type_food'],$row['name']);
			$this->typefood[] = $typefood;
		}
	}
	
	public function getTypefood() {
		return $this->typefood;
	}

	/**
	 * @param multitype: $typefood
	 */
	public function setTypefood($typefood) {
		$this->typefood = $typefood;
	}
	
	public function query_typefood_by_id($place_cod_place){
		$mysql = new MySQL();
		$mysql->MySQL();
		$query = 'SELECT type_food.cod_type_food cod_type_food,type_food.name name FROM type_food , place_has_type_food WHERE type_food.cod_type_food = place_has_type_food.type_food_cod_type_food AND place_has_type_food.place_cod_place ='.$place_cod_place;
		$result =$mysql->query($query);
		return $result;
	}

	
}

class BL_place {

	private $place = array();

	/**
	 * @return the $place
	 */
	public function getPlace() {
		return $this->place;
	}

	/**
	 * @param multitype: $place
	 */
	public function setPlace($place) {
		$this->place = $place;
	}

	/** search with param name(of place)
	 *
	 * @param string $name
	 */
	public function BL_place($param){
		
		if (!is_numeric($param)){
			$this->BL_place_by_name($param);
		}
		else {
			$this->BL_place_by_id($param);
		}
		
		
	}

	
	public function BL_place_by_name($name){
		$result = $this->query_place_by_name($name);
		while ($row = mysql_fetch_array($result,MYSQL_BOTH)) {
			$bl_address = new BL_address();
			$bl_address->BL_address($row['cod_place']);
			$bl_typefood = new BL_typefood();
			$bl_typefood-> BL_typefood($row['cod_place']);
			$place = new place(); 
			$place->place($row['cod_place'],
					$row['name'],
					$row['phone'],
					$row['delivery'],
					$row['website'],
					$row['email'],
					$row['description']);
			$place->setAddress($bl_address->getAddress());
			$place->setTypefood($bl_typefood->getTypefood());
			$this->place[] = $place;
		}
	}
	
	public function BL_place_by_id($id){
		$result = $this->query_place_by_id($id);
		while ($row = mysql_fetch_array($result)) {
			$bl_address = new BL_address();
			$bl_address->BL_address($row['cod_place']);
			$bl_typefood = new BL_typefood();
			$bl_typefood->BL_typefood($row['cod_place']);
			$place = new place();
			$place->place($row['cod_place'],
					$row['name'],
					$row['phone'],
					$row['delivery'],
					$row['website'],
					$row['email'],
					$row['description']);
			$place->setAddress($bl_address);
			$place->setTypefood($bl_typefood);
			$this->place[] = $place;
		}
		
	}
	
	public function query_place_by_name($name){
		$words =explode(" ", $name);
		$mysql= new MySQL();
		$mysql->MySQL();
		$query = 'SELECT cod_place,name,phone,delivery,website,email,description FROM rocotito.place WHERE 1 = 1 ';
		foreach($words as $value){
			$param = " OR UPPER(name) like '%".strtoupper(str_replace("'", "''",$value ))."%'";
			$query = $query.$param;
		}

		$result = $mysql->query($query);
		return $result;
	}

	public function query_place_by_id($cod_place){
		$mysql= new Mysql();
		$mysql->MySQL();
		$query = 'SELECT cod_place,name,phone,delivery,website,email,description FROM place WHERE 1 = 1 ';
		$query = $query.'AND cod_place='.$cod_place;
		$result = $mysql->query($query);
		return $result;
	}
}


?>